﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bdcs.Application.Mappers.Dto;
using Bdcs.Application.Services.IServices;
using Bdcs.Core.Entity;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;

namespace Bdcs.Application.Services
{
    public class UserService: IUserService, ITransient
    {
        private readonly IRepository<Adv_UserInfoMod> _userRepository;

        public UserService(IRepository<Adv_UserInfoMod> userRepository)
        {
            _userRepository = userRepository;
        }

        public PagedList<UserDto> List(int pageIndex, int pageSize)
        {
            var users = @"Select Id ,Name ,Phone ,Email ,Remark From Adv_UserInfo Where ID<>'administrator' ".SqlQuery<UserDto>();
            var pageList = new PagedList<UserDto>();
            int count = users.Count<UserDto>();
            var list = users.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            pageList.PageIndex = pageIndex;
            pageList.PageSize = pageSize;
            pageList.TotalCount = count;
            pageList.Items = list;
            if (count % pageSize == 0)
            {
                pageList.TotalPages = count / pageSize;
            }
            else
            {
                pageList.TotalPages = Convert.ToInt32(count / pageSize) + 1;
            }

            return pageList;

        }
        
        
        public int Add(UserInput userInput)
        {
            var rowEffects =
                @"exec usp_Adv_UserInfo_Save @Id,@Name,@Password,@DeptId,@Phone,@Fax,@Email,@Remark"
                    .SqlNonQuery(new
                    {
                        Id = userInput.id,
                        Name = userInput.name,
                        Password = userInput.password,
                        DeptId = userInput.deptId,
                        Phone = userInput.phone,
                        Fax = userInput.fax,
                        Email = userInput.email,
                        Remark = userInput.remark
                    });
            Console.WriteLine(rowEffects);
            return rowEffects;
            
        }

        public int UpdateCompanyChoose(string userid, string companyIds)
        {
            var rowEffects =
                @"exec usp_Web_UserChangeCompany  @UserID,@CompanyIds"
                    .SqlNonQuery(new
                    {
                        UserID = userid,
                        CompanyIds = companyIds
                    });
            Console.WriteLine(rowEffects);
            return rowEffects;
        }

        public int GetIsExist(string userid)
        {
            var count = _userRepository.Count(u => u.Id == userid);
            return count;
        }

        public Adv_UserInfoMod Delete(string userId)
        {
            Adv_UserInfoMod advUserInfo = new Adv_UserInfoMod();
            advUserInfo.Id = userId;
            return _userRepository.Delete(advUserInfo).Entity;
        }
    }
}